import character_dispatch from "../character/dipatch.js";
import SaveManager from "../save/SaveManager.js";
import FullPathData from "../../eventbus/FullPathData.js";
import BuilderManager from "../Builder/BuilderManager.js";

let first = true

class MapManager {
    constructor(win) {
        this.saveManager = new SaveManager(win)
        this.map_data = this.saveManager.load_data('map-data');
    }

    remove_other_map_data() {
        if (first) {
            first = false;
            return
        }
        for (let i = 0; i < this.saveManager.mapsize; i++) {
            for (let j = 0; j < this.saveManager.mapsize; j++) {
                this.map_data[i][j].other = {}
            }
        }
    }

    isClear(cell_data) {
        // 判断对应的格子的数据是否没有任何资源
        //FIXME 如果新增资源这里需要加入判断语句

        return (
            cell_data.resource === null && cell_data.character === null
            && cell_data.building === null && cell_data.monster === null
        )
    }

    set_source(where) {
        FullPathData.source = where
    }

    set_target(where) {
        FullPathData.target = where
    }

    render(load_aop) {

        try {
            this.remove_other_map_data()
            this.render_resources()
            this.render_character()
            this.render_builder()
            if (load_aop) {

                let aaa = load_aop()
                this.map_data['other'] = aaa
            }
            this.render_other_select()
            this.saveManager.save_data('map-data', this.map_data)
        } catch (e) {

            // 获取请求参数
            let okay = new URLSearchParams(location.search).get('okay')
            if (okay !== '1')
                location.href = "?okay=1"
            else
                throw e
        }
    }

    load_other_select(mod_map_data = {
        color: 'greenyellow',
        data: [[1, 1], [1, 3], [1, 5]]
    }, reload = true) {
        // console.log(mod_map_data)
        if (reload) {
            this.remove_other_map_data()
        }

        this.map_data['other'] = mod_map_data
    }

    render_character() {
        for (let character of character_dispatch.all_character_dispatch) {
            let instance = new character()

            instance = character_dispatch.get_character_save(instance.id, this.saveManager)
            // 找到存档中是否已经有该存档

            if (instance.where.col !== void 0 && instance.where.row !== void 0) {
                // 满足角色
                let {col, row} = instance.where
                this.map_data[row][col].character = instance
                console.log(`【角色渲染】将角色\`${instance.name}\`渲染到${row},${col}上`)
            }
        }

    }

    render_resources() {
        // 目前资源不需要渲染管道
    }

    render_builder() {
        // 渲染建筑数据
        let buildManager = new BuilderManager(window);
        let cities = buildManager.getAllCitiesBySave()
        for (let city of cities) {
            this.map_data[city.row][city.col].building = city
            console.log(`【建筑渲染】将建筑\`${city.save.name}\`渲染到${city.row},${city.col}上`)
        }
    }


    render_other_select() {
        // 渲染其他选择
        let other_select = this.map_data['other']

        if (!other_select) return
        let color = other_select.color
        let data = other_select.data
        for (let [y, x] of data) {
            if (x < 0 || y < 0 || x >= this.saveManager.mapsize || y >= this.saveManager.mapsize) {
                continue
            }
            this.map_data[y][x].other = {
                color
            }
        }
    }
}

export default MapManager